<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务编号" prop="taskNo">
        <el-input
          v-model="queryParams.taskNo"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="任务状态" prop="taskStatus">
            <el-select
              v-model="queryParams.taskStatus"
              placeholder="请选择"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_task_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
      </el-form-item>
      <el-form-item label="用户名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="deptId">
            <el-select
              v-model="queryParams.deptId"
              placeholder="单位"
              clearable
              style="width: 240px"
            >
              <el-option
               v-for="item in deptGroupOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
                  :disabled="item.status == 1"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="启用" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.sys_yes_no_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="模式" prop="pattern">
        <el-select v-model="queryParams.pattern" placeholder="请选择" clearable>
          <el-option
            v-for="dict in dict.type.sys_pattern_type"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['task:personlTask:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:personlTask:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table ref="tables" v-loading="loading" :data="personlTaskList" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" v-if="columns[0].visible" sortable="custom" :sort-orders="['descending', 'ascending']"/>
      <el-table-column label="任务编号" align="center" prop="taskNo" v-if="columns[1].visible" width="170" sortable="custom" :sort-orders="['descending', 'ascending']" />
      <el-table-column label="任务名称" align="center" prop="taskName" v-if="columns[2].visible" />
      <el-table-column label="任务模式" align="center" key="taskType" prop="taskType" v-if="columns[3].visible">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_task_model" :value="scope.row.taskType"/>
        </template>
      </el-table-column>
      <el-table-column label="岗位" align="center" prop="postName" v-if="columns[4].visible" />
      <el-table-column label="用户名称" align="center" prop="nickName" v-if="columns[5].visible"/>
      <el-table-column label="单位" align="center" prop="deptName" v-if="columns[6].visible" />
      <el-table-column label="视频" align="center" prop="url" v-if="columns[7].visible">
      <template slot-scope="scope">
        <div v-if="scope.row.url == null">
          <img src="@/assets/images/video_gray.png" alt="video" width="30">
        </div>
        <div v-else-if="scope.row.url.includes('rtsp:')">
          <a 
          href="javascript:void(0)" 
          @click="rtspvideo(`${$publicURL}video/index.html`, scope.row.taskName , scope.row.id)"
          >
          <img src="@/assets/images/video_blue.png" alt="video" width="30">
          </a>
          </div>
          <div v-else>
          <a :href="scope.row.url" target="_blank">
            <img src="@/assets/images/video_blue.png" alt="video" width="30">
          </a>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="时长" align="center" prop="durationTime" v-if="columns[8].visible"/>
      <el-table-column label="成绩" align="center" prop="grade" v-if="columns[9].visible" sortable="custom" :sort-orders="['descending', 'ascending']" />
       <el-table-column label="终端类型" align="center" prop="terminalType" v-if="columns[10].visible">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_terminal_type"
            :value="scope.row.terminalType"
          />
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatus" v-if="columns[11].visible" >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_task_status"
            :value="scope.row.taskStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="模式" align="center" prop="pattern" v-if="columns[12].visible">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_pattern_type"
            :value="scope.row.pattern"
          />
        </template>
      </el-table-column>
      <el-table-column label="启用" align="center" key="status" prop="status" v-if="columns[13].visible">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                :active-value="0"
                :inactive-value="1"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" v-if="columns[14].visible" />
      <el-table-column label="提交时间" align="center" prop="submitTime" v-if="columns[15].visible" sortable="custom" :sort-orders="['descending', 'ascending']" width="180" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['task:personlTask:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['task:personlTask:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="任务编号：">
          <span>{{form.taskNo}}</span>
        </el-form-item>
        <el-form-item label="用户名称：">
          <span>{{form.nickName}}</span>
        </el-form-item>
        <el-form-item label="单位：">
          <span>{{form.deptName}}</span>
        </el-form-item>
        <el-form-item label="岗位" >
              <el-select v-model="form.postId" placeholder="请选择岗位">
                <el-option
                  v-for="item in postOptions"
                  :key="item.postId"
                  :label="item.postName"
                  :value="item.postId+''"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
          </el-form-item>
        <el-form-item label="启用：">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.sys_yes_no_status"
                  :key="dict.value-0"
                  :label="dict.value-0"
                >{{dict.label}}</el-radio>
              </el-radio-group>
        </el-form-item>
        <el-form-item label="备注：" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPersonlTask, getPersonlTask, delPersonlTask, addPersonlTask, updatePersonlTask,changeUserTaskStatus,getRtsp } from "@/api/task/personlTask";
import { listDept } from "@/api/system/dept";
import { listPost } from "@/api/system/post";

export default {
  name: "PersonlTask",
  dicts: ['sys_yes_no_status','sys_task_status','sys_task_model','sys_terminal_type','sys_pattern_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户任务表格数据
      personlTaskList: [],
      // 单位选项
      deptGroupOptions: [],
      // 岗位选项
      postOptions: [],
      // 默认排序
      defaultSort: {prop: 'taskStatus', order: 'ascending'},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pattern:null,
        taskNo: null,
        taskName: null,
        userId: null,
        deptId: null,
        status: null,
      },

      //列表信息
      columns: [
        { key: 0, label: `ID`, visible: false },
        { key: 1, label: `任务编号`, visible: true },
        { key: 2, label: `任务名称`, visible: true },
        { key: 3, label: `任务模式`, visible: true },
        { key: 4, label: `岗位`, visible: false },
        { key: 5, label: `用户名称`, visible: true },
        { key: 6, label: `成绩`, visible: true },
        { key: 7, label: `视频`, visible: true },
        { key: 8, label: `时长`, visible: false },
        { key: 9, label: `单位`, visible: true },
        { key: 10, label: `终端类型`, visible:false },
        { key: 11, label: `任务状态`, visible: true },
        { key: 12, label: `模式`, visible: false },
        { key: 13, label: `启用`, visible: true },
        { key: 14, label: `备注`, visible: false },
        { key: 15, label: `提交时间`, visible: true },
      ],
      // 表单参数
      form: {},
      //视频流参数
      rtsp: {
        ip:null,
        uuid:null,
      },
      // 表单校验
      rules: {
        postId: [
          { required: true, message: "岗位不能为空", trigger: "blur" }
        ],
        remark: [
          {
            min: 0,
            max: 200,
            message: "备注不能超过200字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getGroupList();
    this.getPostList();
  },
  methods: {
    /** 视频流 */
    rtspvideo(url, taskName,id) {
      getRtsp(id).then(response => {
        this.rtsp = response.data;
        if(this.rtsp.uuid!=""){
          url = url+"?id="+id+"&ip="+this.rtsp.ip+"&uuid="+this.rtsp.uuid+"&taskName="+taskName
          window.open(url, taskName);
        }
      });
    },

    /** 查询用户任务列表 */
    getList() {
      this.loading = true;
      listPersonlTask(this.queryParams).then(response => {
        this.personlTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询单位列表 */
    getGroupList(){
      listDept({pageSize: 1000}).then(response => {
        this.deptGroupOptions = response.data;
      });
    },

  /** 查询职务内容 */
    getPostList() {
      listPost({type:2,pageSize: 1000}).then(response => {
        this.postOptions = response.rows;
      });
    },
  // 状态修改
    handleStatusChange(row) {
      let text = row.status === 1 ? "移除" : "启用";
      this.$modal.confirm('确认要' + text + '"' + row.nickName + '"用户吗？').then(function() {
        return changeUserTaskStatus(row.id,row.taskNo, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 1 ? 0 : 1;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskNo: null,
        userId: null,
        deptId: null,
        remark: null,
        url: null,
        durationTime: null,
        terminalType: null,
        grade: null,
        taskType: null,
        status: 0,
        delFlag: null,
        createTime: null,
        updateTime: null,
        submitTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPersonlTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePersonlTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPersonlTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务ID为"' + ids + '"的数据项？').then(function() {
        return delPersonlTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('task/personlTask/export', {
        ...this.queryParams
      }, `personlTask_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
